package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.imgedit.fliter.PhotoProcessing;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityPicAdjustBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class PicAdjustActivity extends BaseAc<ActivityPicAdjustBinding> implements SeekBar.OnSeekBarChangeListener {

    public static String imgPath = "";
    private Bitmap mImgBitmap, mSaveBmp;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_adjust;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicAdjustBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvAdjustSave.setOnClickListener(this);
        mDataBinding.sbSmooth.setOnSeekBarChangeListener(this);
        mDataBinding.sbSkin.setOnSeekBarChangeListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvAdjustSave:
                saveImg();
                break;
        }
    }

    @Override
    protected void initData() {
        if (imgPath == null || TextUtils.isEmpty(imgPath)) return;
        mImgBitmap = ImageUtils.getBitmap(imgPath);
        mSaveBmp = mImgBitmap;
        mDataBinding.imageViewTouch.setImageBitmap(mImgBitmap);
        mDataBinding.imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.imageViewTouch.setScaleEnabled(false);// 禁用缩放
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                //保存图片
                ImageUtils.save2Album(mSaveBmp, Bitmap.CompressFormat.PNG);
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort(R.string.save_suc);
                startActivity(HomeActivity.class);
            }
        });
    }

    private void doBeautyTask() {
        final int smoothValue = mDataBinding.sbSmooth.getProgress();
        final int skinValue = mDataBinding.sbSkin.getProgress();
        showDialog(getString(com.stark.common.res.R.string.handling));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                Bitmap bitmap = Bitmap.createBitmap(mImgBitmap.copy(
                        Bitmap.Config.ARGB_8888, true));
                PhotoProcessing.handleSmoothAndWhiteSkin(bitmap, smoothValue, skinValue);
                emitter.onNext(bitmap);
            }

            @Override
            public void accept(Bitmap bitmap) {
                dismissDialog();
                if (bitmap == null) {
                    return;
                }
                mSaveBmp = bitmap;
                mDataBinding.imageViewTouch.setImageBitmap(mSaveBmp);
            }
        });
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        doBeautyTask();
    }

}